<?php
namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use App\Model\CanvassBusiness;
use Illuminate\Http\Request;

class CanvassBusinessController extends Controller
{
    public function applyMessage(Request $request){
        $this->validate(
            $request,
            [
                'name' => 'required|string|min:2|max:10',
                'phone' => ['regex:/^(\+?0?86\-?)?((13\d|14[57]|15[^4,\D]|17[3678]|18\d)\d{8}|170[059]\d{7})$/','unique:canvass_business']
            ],
            [
                'required' => ':attribute不能为空',
                'phone.regex' => '手机格式不对'
            ],
            [
                'name' => '名称',
                'phone' => '手机'
            ]
        );

        $canvassBusinessObj = new CanvassBusiness();
        $canvassBusinessObj->name = $request->input('name');
        $canvassBusinessObj->phone = $request->input('phone');
        $canvassBusinessObj->province_id = $request->input('province_id');
        $canvassBusinessObj->city_id = $request->input('city_id');
//        $canvassBusinessObj->area_id = $request->input('area_id');
        $canvassBusinessObj->type = $request->input('type');
        $result = $canvassBusinessObj->save();
        if ($result) {
            return $this->renderJson();
        } else {
            return $this->renderJson([], '-1', 'error');
        }
    }
}